import { Vector2 } from "../vector";  
import { Engine } from "../engine";  
import { CameraEffect } from "./camera-effect";  
import { lerp } from "../utils";  
  
export enum CinematicAction {  
  JUMP = "jump",  
  LAND = "land",  
  CLIMB = "climb",  
  FALL = "fall"  
}  
  
export class ActionTriggeredEffect extends CameraEffect {  
  private action: CinematicAction;  
  private intensity: number;  
  private lastPlayerVelocity: Vector2 = new Vector2();  
  
  constructor(engine: Engine, action: CinematicAction, intensity: number = 1) {  
    super(engine);  
    this.action = action;  
    this.intensity = intensity;  
  }  
  
  apply(basePos: Vector2): Vector2 {  
    if (!this.isActive) return basePos;  
  
    const player = this.engine.player;  
    const playerVel = player.body_.vel;  
    const playerPos = player.body_.pos;  
  
    let offset = new Vector2();  
  
    switch (this.action) {  
      case CinematicAction.JUMP:  
        // 跳跃时相机稍微向上偏移，营造升腾感 
        player.physics.jump(); 
        if (playerVel.y < -2) {  
          offset.y = -Math.abs(playerVel.y) * 3 * this.intensity;  
        } 

        break;  
  
      case CinematicAction.LAND:  
        // 着陆时相机震动并向下偏移  
        if (this.lastPlayerVelocity.y > 3 && Math.abs(playerVel.y) < 1) {  
          const landingForce = this.lastPlayerVelocity.y;  
          offset.y = landingForce * 2 * this.intensity;  
          offset.x = Math.sin(this.engine.time_ / 20) * landingForce * this.intensity;  
        }  
        break;  
  
      case CinematicAction.CLIMB:  
        // 攀爬时相机跟随角色手部动作  
        if (player.physics.mode_ === 2) { // MotionMode.climbing  
          offset.x = Math.sin(this.engine.time_ / 100) * 5 * this.intensity;  
          offset.y = Math.cos(this.engine.time_ / 150) * 3 * this.intensity;  
        }  
        break;  
  
      case CinematicAction.FALL:  
        // 下落时相机加速跟随，营造失重感  
        if (playerVel.y > 2) {  
          offset.y = playerVel.y * 1.5 * this.intensity;  
          offset.x = Math.sin(this.engine.time_ / 80) * 2 * this.intensity;  
        }  
        break;  
    }  
  
    this.lastPlayerVelocity = playerVel.copy();  
  
    return new Vector2(  
      basePos.x + offset.x * (1 - this.progress),  
      basePos.y + offset.y * (1 - this.progress)  
    );  
  }  
}